package com.jxdinfo.hussar.example.common.schedulingtask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.MdUnitMeasureMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdClient;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdUnitMeasure;
import com.jxdinfo.hussar.example.common.schedulingtask.service.MdUnitMeasureService;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdClientVO;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdUnitMeasureVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 单位表 服务实现类
 * </p>
 *
 * @author 乔镔
 * @since 2024-04-02
 */
@Service("com.jxdinfo.hussar.example.common.schedulingtask.service.impl.MdUnitMeasureServiceImpl")
public class MdUnitMeasureServiceImpl extends ServiceImpl<MdUnitMeasureMapper, MdUnitMeasure> implements MdUnitMeasureService {

    private static final Logger logger = LoggerFactory.getLogger(MdUnitMeasureServiceImpl.class);

    @Autowired
    private MdUnitMeasureService mdUnitMeasureService;

    @Autowired
    private MdUnitMeasureMapper mdUnitMeasureMapper;

    @Override
    public void manualSynchronization() {
        logger.info("\n手动同步-单位表-开始同步单位表");
        LocalDateTime nowTime = LocalDateTime.now();
        // 1查询erp所有的单位表信息
        List<MdUnitMeasureVO> allMdUnitMeasures = mdUnitMeasureMapper.selectAllClient();
        // 2单位表信息转换成主键-对象map集合
        Map<Long, MdUnitMeasureVO> allMdUnitMeasureMap = allMdUnitMeasures.stream().collect(Collectors.toMap(MdUnitMeasureVO::getMeasureId, t -> t));
        // 3单位表信息中的id转换成id列表
        List<Long> erpAllMdUnitMeasureIdList = allMdUnitMeasures.stream().map(MdUnitMeasureVO::getMeasureId).collect(Collectors.toList());
        // 4查询所有的mes单位表信息
        QueryWrapper<MdUnitMeasure> mdUnitMeasureWrapper = new QueryWrapper<>();
        mdUnitMeasureWrapper.eq("deleted_flag","0");
        List<MdUnitMeasure> mesMdMeasures = mdUnitMeasureMapper.selectList(mdUnitMeasureWrapper);
        // 5mes中的客户信息转换成id列表,
        List<Long> mesIdList = mesMdMeasures.stream().map(MdUnitMeasure::getMeasureId).collect(Collectors.toList());
        // 6找出erp中有但是mes中没有的id，这写id需要插入
        ArrayList<Long> erpHasMesNoIdList = new ArrayList<>();
        for (int i = 0; i < erpAllMdUnitMeasureIdList.size(); i++) {
            if (!mesIdList.contains(erpAllMdUnitMeasureIdList.get(i))) {
                erpHasMesNoIdList.add(erpAllMdUnitMeasureIdList.get(i));
            }
        }
        if (erpHasMesNoIdList.size() > 0) {
            // 循环创建对象，保存待要插入的结果
            ArrayList<MdUnitMeasure> insertList = new ArrayList<>();
            for (Long insertId : erpHasMesNoIdList) {
                MdUnitMeasureVO mdUnitMeasureVO = allMdUnitMeasureMap.get(insertId);
                MdUnitMeasure mdUnitMeasure = new MdUnitMeasure();
                mdUnitMeasure.setMeasureId(mdUnitMeasureVO.getMeasureId());
                mdUnitMeasure.setMeasureCode(mdUnitMeasureVO.getMeasureCode());     // 编码
                mdUnitMeasure.setMeasureName(mdUnitMeasureVO.getMeasureName());     // 名称
                mdUnitMeasure.setPrimaryFlag(mdUnitMeasureVO.getPrimaryFlag());     // 是否是主单位
                mdUnitMeasure.setChangeRate(mdUnitMeasureVO.getChangeRate());     // 与主单位换算比例
                mdUnitMeasure.setEnableFlag(mdUnitMeasureVO.getEnableFlag());     // 是否启用
                mdUnitMeasure.setScheduleTime(nowTime);
                mdUnitMeasure.setCreateBy(mdUnitMeasureVO.getCreateBy());
                mdUnitMeasure.setCreateTime(mdUnitMeasureVO.getCreateTime());
                mdUnitMeasure.setUpdateBy(mdUnitMeasureVO.getUpdateBy());
                mdUnitMeasure.setUpdateTime(mdUnitMeasureVO.getUpdateTime());
                mdUnitMeasure.setErpUnitId(mdUnitMeasureVO.getMeasureId());
                insertList.add(mdUnitMeasure);
            }
            mdUnitMeasureService.saveBatch(insertList);
        }
        // 7找出erp和mes中都有的，这些需要更新
        ArrayList<Long> updateIdList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (erpAllMdUnitMeasureIdList.contains(mesIdList.get(i))){
                updateIdList.add(mesIdList.get(i));
            }
        }
        if (updateIdList.size()>0) {
            ArrayList<MdUnitMeasure> mdUnitMeasureUpdateList = new ArrayList<>();
            List<MdUnitMeasure> mdUnitMeasureList = mdUnitMeasureMapper.selectBatchIds(updateIdList);
            for (MdUnitMeasure mdUnitMeasure : mdUnitMeasureList) {
                Long erpMeasureId = mdUnitMeasure.getMeasureId();
                MdUnitMeasureVO mdUnitMeasureVO = allMdUnitMeasureMap.get(erpMeasureId);
                mdUnitMeasure.setMeasureCode(mdUnitMeasureVO.getMeasureCode());              // 编码
                mdUnitMeasure.setMeasureName(mdUnitMeasureVO.getMeasureName());              // 名称
                mdUnitMeasure.setEnableFlag(mdUnitMeasureVO.getEnableFlag());              // 生效状态
                mdUnitMeasure.setChangeRate(mdUnitMeasureVO.getChangeRate());     // 与主单位换算比例
                mdUnitMeasure.setEnableFlag(mdUnitMeasureVO.getEnableFlag());     // 是否启用
                mdUnitMeasure.setScheduleTime(nowTime);
                mdUnitMeasure.setCreateBy(mdUnitMeasureVO.getCreateBy());
                mdUnitMeasure.setCreateTime(mdUnitMeasureVO.getCreateTime());
                mdUnitMeasure.setUpdateBy(mdUnitMeasureVO.getUpdateBy());
                mdUnitMeasure.setUpdateTime(mdUnitMeasureVO.getUpdateTime());
                mdUnitMeasureUpdateList.add(mdUnitMeasure);
            }
            mdUnitMeasureService.updateBatchById(mdUnitMeasureUpdateList);
        }
    }
}
